package com.ruoyi.category.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.vo.RouterVo;
import com.ruoyi.category.mapper.SysSubjectMapper;
import com.ruoyi.category.service.ISysSubjectService;

/**
 * 科目 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysSubjectServiceImpl implements ISysSubjectService
{
    @Autowired
    private SysSubjectMapper subjectMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 查询类别列表
     * 
     * @param subject 类别信息
     * @return 类别列表
     */
    @Override
    public List<SysDept> selectSubjectList(SysDept subject)
    {
        List<SysDept> subjectList = deptMapper.selectSubjectList(subject);
        return subjectList;
    }

    /**
     * 构建前端路由所需要的类别
     * 
     * @param subjects 类别列表
     * @return 路由列表
     */
    @Override
    public List<RouterVo> buildSubjects(List<SysDept> subjects)
    {
        List<RouterVo> routers = new LinkedList<RouterVo>();
        for (SysDept subject : subjects)
        {
            RouterVo router = new RouterVo();
            List<SysDept> cSubjects = subject.getChildren();
            if (!cSubjects.isEmpty() && cSubjects.size() > 0)
            {
                router.setAlwaysShow(true);
                router.setRedirect("noRedirect");
                router.setChildren(buildSubjects(cSubjects));
            }
            else if (subject.getParentId().intValue() == 0)
            {
                router.setPath("/");
                List<RouterVo> childrenList = new ArrayList<RouterVo>();
                RouterVo children = new RouterVo();
                children.setComponent(UserConstants.INNER_LINK);
                childrenList.add(children);
                router.setChildren(childrenList);
            }
            routers.add(router);
        }
        return routers;
    }

    /**
     * 构建前端所需要树结构
     * 
     * @param subjects 类别列表
     * @return 树结构列表
     */
    @Override
    public List<SysDept> buildSubjectTree(List<SysDept> subjects)
    {
        List<SysDept> returnList = new ArrayList<SysDept>();
        List<Long> tempList = new ArrayList<Long>();
        for (SysDept dept : subjects)
        {
            tempList.add(dept.getDeptId());
        }
        for (Iterator<SysDept> iterator = subjects.iterator(); iterator.hasNext();)
        {
            SysDept subject = (SysDept) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(subject.getParentId()))
            {
                recursionFn(subjects, subject);
                returnList.add(subject);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = subjects;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     * 
     * @param subjects 类别列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildSubjectTreeSelect(List<SysDept> subjects)
    {
        List<SysDept> subjectTrees = buildSubjectTree(subjects);
        return subjectTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 新增保存类别信息
     *
     * @param subject 类别信息
     * @return 结果
     */
    @Override
    public int insertSubject(SysDept subject)
    {
        return deptMapper.insertDept(subject);
    }

    /**
     * 删除类别管理信息
     * 
     * @param deptId 类别ID
     * @return 结果
     */
    @Override
    public int deleteSubjectById(Long deptId)
    {
        return deptMapper.deleteDeptById(deptId);
    }

    /**
     * 校验类别名称是否唯一
     * 
     * @param subject 类别信息
     * @return 结果
     */
    @Override
    public String checkSubjectNameUnique(SysDept subject)
    {
        Long deptId = StringUtils.isNull(subject.getDeptId()) ? -1L : subject.getDeptId();
        SysDept info = subjectMapper.checkSubjectNameUnique(subject.getDeptName(), subject.getParentId());
        if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }



    /**
     * 根据父节点的ID获取所有子节点
     * 
     * @param list 分类表
     * @param parentId 传入的父节点ID
     * @return String
     */
    public List<SysDept> getChildPerms(List<SysDept> list, int parentId)
    {
        List<SysDept> returnList = new ArrayList<SysDept>();
        for (Iterator<SysDept> iterator = list.iterator(); iterator.hasNext();)
        {
            SysDept t = (SysDept) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId() == parentId)
            {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    /**
     * 递归列表
     * 
     * @param list
     * @param t
     */
    private void recursionFn(List<SysDept> list, SysDept t)
    {
        // 得到子节点列表
        List<SysDept> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysDept tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysDept> getChildList(List<SysDept> list, SysDept t)
    {
        List<SysDept> tlist = new ArrayList<SysDept>();
        Iterator<SysDept> it = list.iterator();
        while (it.hasNext())
        {
            SysDept n = (SysDept) it.next();
            if (n.getParentId().longValue() == t.getDeptId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysDept> list, SysDept t)
    {
        return getChildList(list, t).size() > 0;
    }
}
